from flask import render_template, url_for

from blueprint import page
from model.crud import QueryOneInstance
from model.tables import Blog
from restful.error import RenderPostNotExist

LIMIT=4

@page.route('/front/blog/detail/<int:pid>', methods=['GET'])
def front_blogdetail(pid):
    blog = QueryOneInstance(Blog,{'id':pid})
    if not blog:
        return RenderPostNotExist(pid)

    return render_template('front/blogdetail.html',pid=pid,uid=blog.uid)

@page.route('/front/blog/hot',methods=['GET'])
def front_bloghot():
    return render_template('front/bloglist.html', title='最热前十', url=url_for('postslistbytophot'))

@page.route('/front/blog/recent',methods=['GET'])
def front_blogrecent():
    return render_template('front/bloglist.html', title='最新前十', url=url_for('postslistbytoprecent'))

@page.route('/front/blog/list/by/make/friends',methods=['GET'])
def front_bloglistbymakefriends():
    return render_template('front/bloglistpaginate.html', title='交友',
                           param={
                               'asc':False,
                               'limit':LIMIT,
                               'sort':'交友'
                           }, url=url_for('postslistbysort'))

@page.route('/front/blog/list/by/trade',methods=['GET'])
def front_bloglistbytrade():
    return render_template('front/bloglistpaginate.html', title='交易',
                           param={
                               'asc':False,
                               'limit':LIMIT,
                               'sort':'交易'
                           }, url=url_for('postslistbysort'))

@page.route('/front/blog/list/by/help',methods=['GET'])
def front_bloglistbyhelp():
    return render_template('front/bloglistpaginate.html', title='求助',
                           param={
                               'asc':False,
                               'limit':LIMIT,
                               'sort':'求助'
                           }, url=url_for('postslistbysort'))

@page.route('/front/blog/list/by/similar',methods=['GET'])
def front_bloglistbysimilar():
    return render_template('front/bloglistpaginate.html', title='相似推荐',
                           param={
                               'asc':False,
                               'limit':LIMIT,
                           }, url=url_for('postslistbysimilar'))

@page.route('/front/blog/list/by/following',methods=['GET'])
def front_bloglistbyfollowing():
    return render_template('front/bloglistpaginate.html', title='我的关注',
                           param={
                               'asc':False,
                               'limit':LIMIT,
                           }, url=url_for('postslistbymyfollowing'))